package com.itbaizhan.openhis.controller;
import com.itbaizhan.openhis.dto.DeptDto;
import com.itbaizhan.openhis.service.DeptService;
import com.itbaizhan.openhis.utils.ShiroSecurityUtils;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

/**
 * 科室管理控制器
 */
@RestController
@RequestMapping("system/dept")
@Slf4j
public class DeptController {
	
	@Autowired
	private DeptService deptService;
	
	/**
	 * 科室列表分页查询
	 *
	 * @param deptDto
	 * @return
	 */
	@GetMapping("listDeptForPage")
	public AjaxResult listDeptForPage(DeptDto deptDto) {
		log.info("查询科室列表分页数据，参数：{}", deptDto);
		DataGridView dataGridView = deptService.listDeptForPage(deptDto);
		return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
	}
	
	/**
	 * 添加科室
	 *
	 * @param deptDto
	 * @return
	 */
	@PostMapping("addDept")
	public AjaxResult addDept(@Validated DeptDto deptDto) {
		log.info("添加科室，参数：{}", deptDto);
		deptDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
		return AjaxResult.toAjax(deptService.addDept(deptDto));
	}
	
	/**
	 * 修改科室
	 *
	 * @param deptDto
	 * @return
	 */
	@PutMapping("updateDept")
	public AjaxResult updateDept(@Validated DeptDto deptDto) {
		log.info("修改科室，参数：{}", deptDto);
		deptDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
		return AjaxResult.toAjax(deptService.updateDept(deptDto));
	}
	
	/**
	 * 批量删除科室
	 *
	 * @param deptIds
	 * @return
	 */
	@DeleteMapping("deleteDeptByIds/{deptIds}")
	public AjaxResult deleteDeptByIds(@PathVariable @Validated
								 @NotEmpty(message = "科室ID不能为空") Long[] deptIds) {
		log.info("批量删除科室，参数：{}", deptIds);
		return AjaxResult.toAjax(deptService.deleteDeptByIds(deptIds));
	}
}
